package com.ruoyi.ihome.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ihome.domain.IhHome;
import com.ruoyi.ihome.domain.IhTopic;
import com.ruoyi.ihome.domain.SearchCriteria;
import com.ruoyi.ihome.service.IIhHomeService;
import com.ruoyi.ihome.service.IIhTopicService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 帖子管理Controller
 *
 * @author me
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/ihome/topic")
public class IhTopicController extends BaseController {
    @Autowired
    private IIhTopicService ihTopicService;
    @Autowired
    private IIhHomeService ihHomeService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询帖子管理列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(IhTopic ihTopic) {
        startPage();
        List<IhTopic> list = ihTopicService.selectIhTopicList(ihTopic);
        return getDataTable(list);
    }

    /**
     * 导出帖子管理列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:topic:export')")
    @Log(title = "帖子管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IhTopic ihTopic) {
        List<IhTopic> list = ihTopicService.selectIhTopicList(ihTopic);
        ExcelUtil<IhTopic> util = new ExcelUtil<IhTopic>(IhTopic.class);
        util.exportExcel(response, list, "帖子管理数据");
    }

    /**
     * 获取帖子管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ihome:topic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ihTopicService.selectIhTopicById(id));
    }

    /**
     * 获取合租房信息
     */
    @GetMapping(value = "/gethouse")
    public AjaxResult gethouse() {
        Long housesListLength = redisTemplate.opsForList().size("housesList");
        if (housesListLength == 0 || housesListLength == null) {
            List<IhHome> houses = ihTopicService.gethouse();
            Iterator<IhHome> it = houses.iterator();
            List<Map> list = new ArrayList<>();
            while (it.hasNext()) {
                IhHome ihHome = it.next();
                Map map = new HashMap();
                map.put("label", ihHome.getHouseName());
                map.put("value", ihHome.getId());
                list.add(map);
            }
            redisTemplate.opsForList().rightPushAll("housesList", list);
            return success(list);
        } else {
            List housesList = redisTemplate.opsForList().range("housesList", 0, housesListLength);
            return success(housesList);
        }
    }

    /**
     * 新增帖子管理
     */
//    @PreAuthorize("@ss.hasPermi('ihome:topic:add')")
    @Log(title = "帖子管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IhTopic ihTopic) {
        return toAjax(ihTopicService.insertIhTopic(ihTopic));
    }

    @PostMapping("/insertTopicAndUser")
    public AjaxResult insertTopicAndUser(@RequestBody IhTopic ihTopic) {
        if (ihTopic.getLatitude() == null) {
            Long houseId = ihTopic.getHouseId();
            IhHome ihHome = ihHomeService.selectIhHomeById(houseId);
            ihTopic.setLatitude(ihHome.getLatitude());
            ihTopic.setLongitude(ihHome.getLongitude());
            ihTopic.setCommunityName(ihHome.getHouseName());
            Long status = Long.valueOf(1);
            ihTopic.setStatus(status);
        }
        int res = ihTopicService.insertIhTopic(ihTopic);
        ihTopicService.insertTopicAndUser(ihTopic);
        return AjaxResult.success();
    }

    /**
     * 搜索合租房帖子信息
     */
    @GetMapping(value = "/selectsharedflats")
    public AjaxResult selectSharedFlats(Integer userId) {
        List<IhTopic> ihTopics = ihTopicService.selectSharedFlats(userId);
        return AjaxResult.success(ihTopics);

    }
    /**
     * 搜索求租房帖子信息
     */
    @GetMapping("/selectaskingforrent")
    public AjaxResult selectAskingForRent(Integer userId) {
        List<IhTopic> ihTopics = ihTopicService.selectAskingForRent(userId);
        return success(ihTopics);

    }

    /**
     * 根据userId搜索求帖子
     */
    @GetMapping("/selectalltopic")
    public AjaxResult selectAllTopic(@RequestParam(required = false) Integer userId, @RequestParam(required = false) String serchValue) {
        List<IhTopic> ihTopics = ihTopicService.selectAllTopic(userId, serchValue);
        return AjaxResult.success(ihTopics);
    }

    /**
     * 搜索合租房帖子详细信息
     */
    @GetMapping("/selectsharedflatstopicdetails")
    public AjaxResult selectSharedFlatsTopicDetails(Integer topicId, Integer userId) {
        IhTopic ihTopic = ihTopicService.selectSharedFlatsTopicDetails(topicId, userId);
        return success(ihTopic);
    }

    /**
     * 搜索求租房帖子详细信息
     */
    @GetMapping("/selectaskingforrenttopicdetails")
    public AjaxResult selectAskingForRentTopicDetails(Integer topicId, Integer userId) {
        IhTopic ihTopic = ihTopicService.selectAskingForRentTopicDetails(topicId, userId);
        return success(ihTopic);
    }

    ;

    /**
     * 修改帖子管理
     */
    @PreAuthorize("@ss.hasPermi('ihome:topic:edit')")
    @Log(title = "帖子管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IhTopic ihTopic) {
        return toAjax(ihTopicService.updateIhTopic(ihTopic));
    }

    /**
     * 删除帖子管理
     */
    @PreAuthorize("@ss.hasPermi('ihome:topic:remove')")
    @Log(title = "帖子管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ihTopicService.deleteIhTopicByIds(ids));
    }

    @GetMapping("/attentiontopic")
    public AjaxResult attentionTopic(Long userId, Long topicId) {
        System.out.println(userId + "," + topicId);
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("topicId", topicId);
        int res = ihTopicService.attentionTopic(map);
        return AjaxResult.success(res);
    }

    @GetMapping("/cancelattention")
    public AjaxResult cancelAttention(Integer userId, Integer topicId) {
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("topicId", topicId);
        System.out.println(map);
        int res = ihTopicService.cancelAttention(map);
        return AjaxResult.success(res);
    }
}
